package com.example.myblog1_0.controller;

import com.example.myblog1_0.model.Comment;
import com.example.myblog1_0.service.ArticleService;
import com.example.myblog1_0.service.CommentService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author: xc
 * @Date: 2018/12/20 20:31
 * @Description:
 **/
@Controller
public class ShowController {
    @Autowired
    ArticleService articleService;
    @Autowired
    CommentService commentService;
    @GetMapping("/findArticle")
    public String findArticle(@RequestParam("articleID") String articleId, HttpServletRequest request, HttpServletResponse response, Model model){
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        request.getSession().removeAttribute("lastUrl");

        Map<String,String> articleMap = articleService.findArticleById(articleId);
        model.addAttribute("articleTitle",articleMap.get("articleTitle"));

        response.setHeader("articleID",articleId);

        return "show";
    }

    @PostMapping("/getArticleByArticleId")
    public @ResponseBody JSONObject getArticleByArticleId(@RequestParam("articleID")String id){
        return articleService.getArticleByArticleId(id);
    }


}
